package com.kly.user.serviceImpl;

import com.fqgj.common.api.Response;
import com.google.common.collect.Maps;
import com.kly.user.db.dao.BlindBoxPrizeConfigDao;
import com.kly.user.db.entity.BlindBoxPrizeConfigEntity;
import com.kly.user.dto.BindBoxDto;
import com.kly.user.dto.PrizeDto;
import com.kly.user.service.BlindBoxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("blindBoxService")
public class BlindBoxServiceImpl implements BlindBoxService {


    @Autowired
    private BlindBoxPrizeConfigDao blindBoxPrizeConfigDao;

    @Override
    public Response<PrizeDto> getPrize(String userCode) {

        PrizeDto prizeDto = new PrizeDto();
        //判断是否是会员
        Map<String, Object> params = Maps.newHashMap();
        params.put("userMember", 0);
        params.put("deleted", 0);
        List<BlindBoxPrizeConfigEntity> blindBoxPrizeConfigEntities = blindBoxPrizeConfigDao.selectByParams(params);
        double cumProbability = 0;
        for (BlindBoxPrizeConfigEntity blindBoxPrizeConfigEntity : blindBoxPrizeConfigEntities) {
            cumProbability += blindBoxPrizeConfigEntity.getRatio();
            blindBoxPrizeConfigEntity.setRatio(cumProbability);
        }

        double randomValue = Math.random();
        Integer prize = 0;
        Integer prizeCount = 0;
        for (BlindBoxPrizeConfigEntity blindBoxPrizeConfigEntity : blindBoxPrizeConfigEntities) {
            if (randomValue < blindBoxPrizeConfigEntity.getRatio()) {
                prize = blindBoxPrizeConfigEntity.getPrize();
                prizeCount = blindBoxPrizeConfigEntity.getPrizeCount();
                break;
            }
        }
        prizeDto.setUserCode(userCode);
        prizeDto.setPrize(prize);
        prizeDto.setPrizeCount(prizeCount);
        //存入缓存

        return Response.ok(prizeDto);
    }

    @Override
    public void addPrize(String userCode) {
        //读取缓存


    }

    @Override
    public Response<BindBoxDto> bindBoxInfo(String userCode) {

        return null;
    }

    @Override
    public void sendAmbiguous(String userCode, String modelUserCode) {

    }

    @Override
    public void ambiguousMatch(String userCode, String modelUserCode) {

    }

    @Override
    public Response<List<String>> barrage(String userCode) {

        return null;
    }


}
